/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm.InPutDlg;

import Data.ThematicData;
import intelligenttm.ITMCarto.BaseData;
import intelligenttm.SourceFile.FenQuALShow;

import intelligenttm.SourceFile.RibbonShow;
import intelligenttm.SourceFile.SymbolShow;
import intelligenttm.SourceFile.SymbolString;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import me.aviator.entity.HibernateUtil;
import me.aviator.entity.Ribbon;
import me.aviator.entity.Symbol;
import me.aviator.entity.myColor;
import me.aviator.service.impl.RibbonService;
import me.aviator.service.impl.SymbolService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * FXML Controller class
 *
 * @author 望舒凌霄
 */
public class AddFenQuDlgController implements Initializable {
    @FXML
    private Button BtnCancel;
    @FXML
    private Button BtnEnsure;
    @FXML
    private MenuButton MenuType;
    @FXML
    private MenuItem BingZhuangItem;
    @FXML
    private MenuItem TiaoXingItem;
    @FXML
    private MenuItem ZheXianItem;
    @FXML
    private MenuItem ZiDingItem;
    @FXML
    private ImageView ShowSample;
    @FXML
    private AnchorPane RootPane;
    @FXML
    private TableView<FenQuALShow> TableAC;
    @FXML
    private TableColumn<FenQuALShow, String> TCAttribute;
    @FXML
    private TableColumn<FenQuALShow, Rectangle> TCLegend;
    @FXML
    private ComboBox<String> AddMenu;
    @FXML
    private Button BtnDelete;
    @FXML
    private TableView<RibbonShow> TableRibbonShow;
    @FXML
    private TableColumn<RibbonShow, HBox> TCRibbonShow;
    @FXML
    private CheckBox ChBIs3D;
    
    private String FenQuType="饼状图";
    private Image FenQuTypeS=new Image("Pic/TypeSamplePic/饼状图.jpg");
    private String CountNum="";
    
    boolean Is3D=false;
    private List<BaseData> BDList=null;
    private List<String> AttributeList=new ArrayList<>();
    private int LevelNum=0;
    private ObservableList<FenQuALShow> FenQuALList =  FXCollections.observableArrayList();
    private ObservableList<RibbonShow> RibbonShowList =  FXCollections.observableArrayList();
    private int WhichBtn=0;
    private Ribbon RibbonColor=null;
    private List<Ribbon> RList=new ArrayList<>();
    private RibbonService ribbonService;
    private SymbolService symbolService;
    
    private Map<String,String> conditions;
    
    @FXML
    private AnchorPane APBingZhuang;
    @FXML
    private AnchorPane APZiDingYi;
    @FXML
    private TableView<SymbolShow> TBVSymbol;
    @FXML
    private TableColumn<SymbolShow, String> TBCSymbolmane;
    @FXML
    private TableColumn<SymbolShow, SwingNode> TBCSymbol;
    @FXML
    private ChoiceBox<String> CBXZiDingYiA;
    @FXML
    private ChoiceBox<String> CBXZiDingYiB;
    @FXML
    private ChoiceBox<String> CBXZiDingYiShuXing;
    
    
    private String ZiDingYiLeiBieA="社会经济";
    private String ZiDingYiLeiBieB="综合经济";
    private Symbol ChoosedSymbol=null;
    private List<Symbol> SymbolList=new ArrayList<>();
    private ObservableList<SymbolShow> SymbolShowList =  FXCollections.observableArrayList();
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        APBingZhuang.setVisible(true);
        APZiDingYi.setVisible(false);
        SessionFactory sf=HibernateUtil.getSessionFactory();
        Session sess=sf.openSession();
        Transaction tx=sess.beginTransaction();
        
        ribbonService=new RibbonService(sess);
        symbolService=new SymbolService(sess);
        
        conditions=new HashMap<>();
        
        ShowSample.setImage(FenQuTypeS);
        TCAttribute.setCellValueFactory(new PropertyValueFactory<>("AttributeString"));
        TCLegend.setCellValueFactory(new PropertyValueFactory<>("Rect"));
        TCRibbonShow.setCellValueFactory(new PropertyValueFactory<>("CHB"));
        
        TBCSymbolmane.setCellValueFactory(new PropertyValueFactory<>("name"));
        TBCSymbol.setCellValueFactory(new PropertyValueFactory<>("SN"));
    }    
    
    public void SetBaseData(List<BaseData> tBDList){
        BDList=tBDList;
        ObservableList<String> ALIST=FXCollections.observableArrayList();
        for(int i=0;i<BDList.size();i++){
            ThematicData TD= BDList.get(i).getData();
            List<String>Title=TD.GetTitleString();
            for (int j=0;j<Title.size()-1;j++){
                ALIST.add(Title.get(j));
            }
        }
        AddMenu.setItems(ALIST);
        //AddMenu.getSelectionModel().select(0); 
        CBXZiDingYiShuXing.setItems(ALIST);
        
        AddMenu.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            String AttributeString=AddMenu.getItems().get(newv.intValue());
            AttributeList.add(AttributeString);
            FenQuALShow FQALS=new FenQuALShow(AttributeString);
            FenQuALList.add(FQALS);
            LevelNum=FenQuALList.size();
            TableAC.setItems(FenQuALList);
            SetColorRibbon();
            if(RibbonColor!=null){
                setColorBySelecedRibbon();
            }
            //AddMenu.getSelectionModel().select(newv.intValue());
        });
        
        
        //自定义
        SymbolString SymbolS=new SymbolString();
        String[] S=null;
        S=SymbolS.getFirstClassification();
        ObservableList<String> FenLeiA=FXCollections.observableArrayList();
        FenLeiA.addAll(Arrays.asList(S));
        CBXZiDingYiA.setItems(FenLeiA);
        CBXZiDingYiA.getSelectionModel().select(0);
        SetFenLeiB();
        CBXZiDingYiB.getSelectionModel().select(0);
        
        CBXZiDingYiA.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            ZiDingYiLeiBieA=CBXZiDingYiA.getItems().get(newv.intValue());
            SetFenLeiB();
            CBXZiDingYiB.getSelectionModel().select(0);
            int Index=newv.intValue();
            if(Index<0){
                Index=0;
            }
            ZiDingYiLeiBieB=CBXZiDingYiB.getItems().get(Index);
            SetSymbolShowList();
        });
        CBXZiDingYiB.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            int Index=newv.intValue();
            if(Index<0){
                Index=0;
            }
            ZiDingYiLeiBieB=CBXZiDingYiB.getItems().get(Index);
            
            SetSymbolShowList();
        });
        CBXZiDingYiShuXing.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            String AttributeString=CBXZiDingYiShuXing.getItems().get(newv.intValue());
            AttributeList.clear();
            AttributeList.add(AttributeString);
        });
        
    }
    
    private void SetSymbolShowList(){
        //ZiDingYiLeiBieA
        //ZiDingYiLeiBieB
        //SymbolList=null;
        if(ZiDingYiLeiBieA.equals("全部"))
        {
            conditions.remove("catagoryA");
        }
        else{
            conditions.put("catagoryA",ZiDingYiLeiBieA);
        }
        if(ZiDingYiLeiBieB.equals("全部"))
            {
                conditions.remove("catagoryB");
            }
            else{
                conditions.put("catagoryB",ZiDingYiLeiBieB);
            }
        SymbolShowList.clear();
        SymbolList=symbolService.findSymbol(conditions);
        for (int i=0;i<SymbolList.size();i++){
            SymbolShow TS=new SymbolShow(SymbolList.get(i));
            SymbolShowList.add(TS);
        }
        TBVSymbol.setItems(SymbolShowList);
        if(!SymbolShowList.isEmpty()){
            TBVSymbol.getSelectionModel().select(0);
        }
    }
            
    private void SetFenLeiB(){
        SymbolString SymbolS=new SymbolString();
        String[] S=null;
        ObservableList<String> FenLeiB=FXCollections.observableArrayList();
        switch (ZiDingYiLeiBieA){
            case "全部":
                FenLeiB.clear();
                S=SymbolS.getSecondClassification1();
               
                FenLeiB.addAll(Arrays.asList(S));
                S=SymbolS.getSecondClassification2();
                for(int i=1;i<S.length;i++){
                    FenLeiB.add(S[i]);
                }
                S=SymbolS.getSecondClassification3();
                for(int i=1;i<S.length;i++){
                    FenLeiB.add(S[i]);
                }
                S=SymbolS.getSecondClassification4();
                for(int i=1;i<S.length;i++){
                    FenLeiB.add(S[i]);
                }
                S=SymbolS.getSecondClassification5();
                for(int i=1;i<S.length;i++){
                    FenLeiB.add(S[i]);
                }
                break;
            case "自然条件":
                FenLeiB.clear();
                S=SymbolS.getSecondClassification1();
                FenLeiB.addAll(Arrays.asList(S));
                break;
            case "自然资源":
                FenLeiB.clear();
                S=SymbolS.getSecondClassification2();
                FenLeiB.addAll(Arrays.asList(S));
                break;
            case "基础设施":
                FenLeiB.clear();
                S=SymbolS.getSecondClassification3();
                FenLeiB.addAll(Arrays.asList(S));
                break;
            case "社会经济":
                FenLeiB.clear();
                S=SymbolS.getSecondClassification4();
                FenLeiB.addAll(Arrays.asList(S));
                break;
            case "生态环境":
                FenLeiB.clear();
                S=SymbolS.getSecondClassification5();
                FenLeiB.addAll(Arrays.asList(S));
                break;
        }
        CBXZiDingYiB.setItems(FenLeiB);
    }
    
    
    public void SetColorRibbon(){
        RList.clear();
        RList=ribbonService.ListByNum(LevelNum);
        //记得判断是否非空；
        
        if(!RList.isEmpty()){
            RibbonShowList.clear();
            for (int i=0;i<RList.size();i++){
                RibbonShow RS=new RibbonShow(RList.get(i));
                RibbonShowList.add(RS);
            }
            TableRibbonShow.setItems(RibbonShowList);
            TableRibbonShow.getSelectionModel().select(0);
            RibbonColor=RList.get(TableRibbonShow.getSelectionModel().getSelectedIndex());
        }
    }
    
    public void setColorBySelecedRibbon(){
        List<myColor> MCL=RibbonColor.getColorlist();
        for (int i=0;i<FenQuALList.size();i++){
            FenQuALList.get(i).setRectangleFill(MCL.get(i));
        }
    }
    @FXML
    private void HandleTableRibbonShow(MouseEvent event) {
        RibbonColor=RList.get(TableRibbonShow.getSelectionModel().getSelectedIndex());
        setColorBySelecedRibbon();
    }
    
    @FXML
    private void HandleBtnDelete(ActionEvent event) {
        int index=TableAC.getSelectionModel().getSelectedIndex();
        if (index>=0){
            FenQuALList.remove(index);
            AttributeList.remove(index);
            LevelNum=FenQuALList.size();
            TableAC.setItems(FenQuALList);
            SetColorRibbon();
            if(RibbonColor!=null){
                setColorBySelecedRibbon();
            }
        }
    }
    
    @FXML
    private void HandleBtnCancel(ActionEvent event) {
        WhichBtn=0;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnEnsure(ActionEvent event) {
        
        switch (FenQuType){
            case "饼状图":
                List<myColor> MCL=new ArrayList<>();
                for(int i=0;i<FenQuALList.size();i++){
                    MCL.add(FenQuALList.get(i).getMyColor());
                }
                RibbonColor.setColorlist(MCL);
                RibbonColor.setNum(MCL.size());
                break;
            case "自定义":
                int Index=TBVSymbol.getSelectionModel().getFocusedIndex();
                if(Index<0){
                    Index=0;
                }
                ChoosedSymbol=SymbolList.get(Index);
                break;
        }
        WhichBtn=1;
        RootPane.getScene().getWindow().hide();
    }
    
    
    @FXML
    private void HandleBingZhuangItem(ActionEvent event) {
        FenQuType="饼状图";
        AttributeList.clear();
        APBingZhuang.setVisible(true);
        APZiDingYi.setVisible(false);
        //SetBaseData(BDList);
        FenQuTypeS=new Image("Pic/TypeSamplePic/"+FenQuType+".jpg");
        ShowSample.setImage(FenQuTypeS);
        MenuType.setText(FenQuType);
    }

    @FXML
    private void HandleTiaoXingItem(ActionEvent event) {
        FenQuType="条形图";
        AttributeList.clear();
        APBingZhuang.setVisible(true);
        APZiDingYi.setVisible(false);
        //SetBaseData(BDList);
        FenQuTypeS=new Image("Pic/TypeSamplePic/"+FenQuType+".jpg");
        ShowSample.setImage(FenQuTypeS);
        MenuType.setText(FenQuType);
    }

    @FXML
    private void HandleZheXianItem(ActionEvent event) {
        FenQuType="折线图";
        AttributeList.clear();
        //SetBaseData(BDList);
        FenQuTypeS=new Image("Pic/TypeSamplePic/"+FenQuType+".jpg");
        ShowSample.setImage(FenQuTypeS);
        MenuType.setText(FenQuType);
    }

    @FXML
    private void HandleZiDingItem(ActionEvent event) {
        FenQuType="自定义";
        AttributeList.clear();
        APBingZhuang.setVisible(false);
        APZiDingYi.setVisible(true);
        //SetBaseData(BDList);
        FenQuTypeS=new Image("Pic/TypeSamplePic/"+FenQuType+".jpg");
        ShowSample.setImage(FenQuTypeS);
        MenuType.setText(FenQuType);
    }


    @FXML
    private void HandleChBIs3D(ActionEvent event) {
        Is3D=ChBIs3D.isSelected();
    }

    public ImageView getShowSample() {
        return ShowSample;
    }

    public void setShowSample(ImageView ShowSample) {
        this.ShowSample = ShowSample;
    }

    public String getFenQuType() {
        return FenQuType;
    }

    public void setFenQuType(String FenQuType) {
        this.FenQuType = FenQuType;
    }

    public String getCountNum() {
        return CountNum;
    }

    public void setCountNum(String CountNum) {
        this.CountNum = CountNum;
    }

    public List<String> getAttributeList() {
        return AttributeList;
    }

    public void setAttributeList(List<String> AttributeList) {
        this.AttributeList = AttributeList;
    }

    public int getWhichBtn() {
        return WhichBtn;
    }

    public void setWhichBtn(int WhichBtn) {
        this.WhichBtn = WhichBtn;
    }

    public Ribbon getRibbonColor() {
        return RibbonColor;
    }

    public void setRibbonColor(Ribbon RibbonColor) {
        this.RibbonColor = RibbonColor;
    }

    public boolean isIs3D() {
        return Is3D;
    }  

    public Symbol getChoosedSymbol() {
        return ChoosedSymbol;
    }

    public void setIs3D(boolean Is3D) {
        this.Is3D = Is3D;
    }
    
}
